package com.lovo.stock.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.stock.bean.*;
import com.lovo.stock.constant.MessageCode;
import com.lovo.stock.service.*;
import com.lovo.stock.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/sale")
public class SaleController {

    @Autowired
    ISacartService sacartService;
    @Autowired
    ISadetailService sadetailService;
    @Autowired
    ISaleService saleService;
    @Autowired
    IGoodService goodService;
    @Autowired
    IJournalService journalService;

    /**
     * 查询当前用户购物车列表
     *
     * @return
     */
    @RequestMapping("/querySacart")
    public ResponseResult<List<SacartBean>> querySacart(HttpServletRequest request) {
        ResponseResult<List<SacartBean>> responseResult=new ResponseResult<>();
        try {
            UserBean userBean = (UserBean) request.getSession().getAttribute("user");
            responseResult.setData(sacartService.querySacart(userBean.getUserId()));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 添加商品到购物车
     *
     * @param sacartBean
     */
    @RequestMapping("/addSacart")
    public ResponseResult addSacart(HttpServletRequest request,@RequestBody SacartBean sacartBean) {
        ResponseResult responseResult=new ResponseResult<>();
        try {
            //设置购物车中商品的userId
            UserBean userBean= (UserBean) request.getSession().getAttribute("user");
            sacartBean.setSacartUserId(userBean.getUserId());
            sacartService.addSacart(sacartBean);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;

    }

    /**
     * 将商品移出购物车
     *
     * @param id
     */
    @RequestMapping("/deleteSacart")
    public ResponseResult deleteSacart(int id) {
        ResponseResult responseResult=new ResponseResult<>();
        try {
            sacartService.deleteSacart(id);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 更新购物车信息
     *
     * @param sacartBean
     */
    @RequestMapping("/updateSacart")
    public ResponseResult updateSacart(@RequestBody SacartBean sacartBean) {
        ResponseResult responseResult=new ResponseResult<>();
        try {
            sacartService.updateSacart(sacartBean);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 出库
     *
     * @return
     */
    @RequestMapping("/Issue")
    public ResponseResult Issue(HttpServletRequest request, String sale, String goods) {
        SaleBean saleBean= JSONObject.parseObject(sale,SaleBean.class);
        List<GoodBean> goodBeans=JSONObject.parseArray(goods,GoodBean.class);
        //获取出库库时间
        saleBean.setSaleDate(LocalDate.now().toString());
        //设置商品详情
        for (int i = 0; i < goodBeans.size(); i++) {
            saleBean.getSadetailBeans().get(i).setSadetailCategory((goodService.queryGoodByGoodNum(goodBeans.get(i).getGoodNum())).getGoodCategory());
        }
        ResponseResult responseResult=new ResponseResult<>();
        try {
            //添加销售信息
            sadetailService.addSaleDetail(saleBean);
            //更改商品信息
            goodService.reduceGoods(goodBeans);
            //清空用户购物车
            UserBean userBean = (UserBean) request.getSession().getAttribute("user");
            sacartService.deleteSacartByUserId(userBean);
            //记录日志
            journalService.addJournal("进行了出库操作",userBean);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 分页查询销售信息
     * @param pageInfo
     * @return
     */
    @RequestMapping("/querySaleByPage")
    public ResponseResult<IPage<SaleBean>> querySaleByPage(@RequestBody PageInfo<SaleBean> pageInfo){
        ResponseResult<IPage<SaleBean>> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(saleService.querySaleByPage(pageInfo));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }


    /**
     * 查询销售详细信息
     * @param id
     * @return
     */
    @RequestMapping("/querySaleById")
    public ResponseResult<SaleBean> querySaleById(int id){
        ResponseResult<SaleBean> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(saleService.querySaleById(id));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }
}
